package com.code.wanmi.controller;


import com.alibaba.fastjson.JSON;
import com.code.wanmi.entity.other.ResponseBean;
import com.code.wanmi.entity.other.RoleMenuDto;
import com.code.wanmi.entity.wm_user.WmSysMenu;
import com.code.wanmi.entity.wm_user.WmSysRole;
import com.code.wanmi.entity.wm_user.WmSysUser;
import com.code.wanmi.entity.wm_user.dto.SysUserDto;
import com.code.wanmi.service.IWmSysRoleService;
import com.code.wanmi.service.IWmSysUserService;
import com.code.wanmi.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 谢晓峰
 * @since 2019-12-05
 */
@RestController
@RequestMapping("/user")
public class WmSysUserController {

    @Autowired
    private IWmSysUserService iWmSysUserService;
    @Autowired
    private IWmSysRoleService iWmSysRoleService;

/**查询用户列表*/
    @RequestMapping(value = "/select",method = RequestMethod.GET)
    public String selectUsers(){
        List<WmSysUser>list = iWmSysUserService.selectlist();
        return JSON.toJSONString(new ResponseBean(200,"success",list));
    }

    /**删除某个用户
     * **/
    @RequestMapping(value = "/remove",method = RequestMethod.PATCH)
    public String removeUser(@RequestBody WmSysUser wmSysUser){
        iWmSysUserService.remove(wmSysUser.getUserId());
        return JSON.toJSONString(new ResponseBean(200,"success"));
    }

    /**
     * 添加用户操作及分配角色
     * &对已有用户添加角色
     * @param dto
     * @return
     */
    @RequestMapping(value = "/insertSysUser",method = RequestMethod.POST)
    public String insertUser(@RequestBody SysUserDto dto){
        WmSysUser user = dto.getWmSysUser();
        WmSysRole role = dto.getWmSysRole();
        if (iWmSysUserService.selectUser(user)==null){
            iWmSysUserService.insertUser(user);
            user = iWmSysUserService.selectUser(user);
            return JSON.toJSONString(new ResponseBean(200,"success",iWmSysRoleService.insertRole(role, user)));

        }
        return JSON.toJSONString(new ResponseBean(200,"success",iWmSysRoleService.insertRole(role, user)));
    }

//    @RequestMapping(value = "/updateUserRole",method = RequestMethod.PUT)
//    public String updateUserRole(@RequestBody SysUserDto dto){
//        WmSysUser user = dto.getWmSysUser();
//        WmSysRole role = dto.getWmSysRole();
//        iWmSysRoleService.updateUserRole(user,role);
//        return JSON.toJSONString(new ResponseBean(200,"success",iWmSysRoleService.updateUserRole(user,role)));
//    }

/**登录*/
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestBody WmSysUser user){
        WmSysUser wmSysUser = iWmSysUserService.selectUserByUsernameAndPassword(user);
        if(wmSysUser!=null) {
            if (iWmSysUserService.updateLoginIP(user)>0) {
                ResponseBean responseBean = new ResponseBean(200,"success");
                responseBean.setData(JWTUtil.sign(wmSysUser.getLoginName(),wmSysUser.getPassword(),wmSysUser.getUserId()));
                return JSON.toJSONString(responseBean);
            }
        }
        return JSON.toJSONString(new ResponseBean(402,"fail"));
    }

    /**
     * 通过用户ID查询用户权限信息
     * @param uid
     * @return
     */
    @RequestMapping(value = "/userInfo",method = RequestMethod.GET)
    String userInfo(@RequestParam("uid") Long uid) {
                Set<RoleMenuDto> set = new HashSet<>();
                ResponseBean responseBean = new ResponseBean(200,"success");
                Set<WmSysRole> roles = iWmSysRoleService.getRoleNameByUserId(uid);//根据用户ID查询角色
                for (WmSysRole role : roles) {
                    List<WmSysMenu> power= iWmSysRoleService.getRoleById(role.getRoleId());//根据角色ID查询菜单
                    HashSet<WmSysMenu> menus = new HashSet<>();
                    for (WmSysMenu wmSysMenu : power) {
                        menus.add(wmSysMenu);
                    }
                    set.add(new RoleMenuDto(role,menus));
                }
                responseBean.setData(set);
        return JSON.toJSONString(responseBean);
    }

    /**修改用户信息*/
    @RequestMapping(value = "/insertSysUser",method = RequestMethod.PUT)
    String updateUser(@RequestBody WmSysUser wmSysUser) {
       return JSON.toJSONString(new ResponseBean(200,"success",iWmSysUserService.updateUser(wmSysUser)));
    }

    /**通过用户id查询用户角色信息*/
    @RequestMapping(value = "/getrole",method = RequestMethod.GET)
    Set<WmSysRole> getRoleNameByUserId(@RequestParam("uid") Long uid) {
        return iWmSysRoleService.getRoleNameByUserId(uid);
    };

    /**通过用户登录名和ID查询用户信息*/
    @RequestMapping(value = "/selectUserByIdAndLoginName",method = RequestMethod.GET)
    WmSysUser selectUserByIdAndLoginName(@RequestBody WmSysUser wmSysUser) {
        return iWmSysUserService.selectUserByIdAndLoginName(wmSysUser);
    }
}
